package com.example.liulongjin;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

@SuppressWarnings("ALL")
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private final ArrayList<IpBean> arrayList;
    private OnItemClickListener onItemClickListener = null;

    public MainAdapter(ArrayList<IpBean> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        IpBean bean = arrayList.get(position);
        holder.ipTextView.setText(bean.getIp());
        holder.addressTextView.setText(bean.getAddr());

        holder.queryTextView.setOnClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onQuery(position, bean);
        });

        holder.mainLinearLayout.setOnClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onClick(position, bean);
        });

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {

        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_main, group, false);
        return new ViewHolder(view);

    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    @SuppressWarnings("unused")
    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    @SuppressWarnings("unused")
    public interface OnItemClickListener {

        void onQuery(int position, IpBean bean);

        void onClick(int position, IpBean bean);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private LinearLayoutCompat mainLinearLayout;
        private AppCompatTextView ipTextView;
        private AppCompatTextView addressTextView;
        private AppCompatTextView queryTextView;

        private ViewHolder(View view) {
            super(view);
            mainLinearLayout = view.findViewById(R.id.mainLinearLayout);
            ipTextView = view.findViewById(R.id.ipTextView);
            addressTextView = view.findViewById(R.id.addressTextView);
            queryTextView = view.findViewById(R.id.queryTextView);
        }

    }

}
